package com.vainycos.lotterysearch.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.vainycos.lotterysearch.enums.LotteryEnum;
import com.vainycos.lotterysearch.mapper.LuckyLotteryMapper;
import com.vainycos.lotterysearch.model.LuckyLottery;
import com.vainycos.lotterysearch.service.notify.DingNotify;
import com.vainycos.lotterysearch.utils.FreeApiUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author: Vainycos
 * @description
 * @date: 2022/11/16 18:07
 */
@Slf4j
@Service
public class LuckyLotteryService extends ServiceImpl<LuckyLotteryMapper, LuckyLottery> {

    @Resource
    private LuckyLotteryMapper luckyLotteryMapper;

    @Lazy
    @Autowired
    private DingNotify dingNotify;

    /**
     * 根据彩票类型查询最近一期的开奖信息
     * @param lotteryEnum 彩票类型
     * @return
     */
    private LuckyLottery searchLatestLotteryByType(LotteryEnum lotteryEnum){
        return luckyLotteryMapper.selectOne(new LambdaQueryWrapper<LuckyLottery>()
                .eq(LuckyLottery::getCode, lotteryEnum.type)
                .orderByDesc(LuckyLottery::getTime)
                .last("limit 1")
        );
    }

    /**
     * 根据彩票期号和类型查询开奖信息
     * @param periodId 期号
     * @param type 彩票类型
     * @return
     */
    private LuckyLottery searchLatestLotteryByTypeAndPeriodId(int periodId, String type){
        return luckyLotteryMapper.selectOne(new LambdaQueryWrapper<LuckyLottery>()
                .eq(LuckyLottery::getExpect, periodId)
                .eq(LuckyLottery::getCode, type)
                .orderByDesc(LuckyLottery::getTime)
                .last("limit 1")
        );
    }

    /**
     * 根据期号和类型查询开奖信息并新增
     * @param periodId 期号
     * @param type 类型
     * @return
     */
    public boolean insertByPeriodAndType(int periodId, String type){
        LuckyLottery queryResult = searchLatestLotteryByTypeAndPeriodId(periodId, type);
        if (queryResult != null){
            log.error("不允许重复入库开奖信息, 期号->{}, 彩票类型->{}", periodId, type);
            return false;
        }else {
            LuckyLottery luckyLottery = FreeApiUtil.luckyLottery(periodId, LotteryEnum.byType(type));
            if (luckyLottery != null){
                return insert(luckyLottery);
            }
        }
        return false;
    }

    /**
     * 新增
     * @param luckyLottery
     * @return
     */
    private boolean insert(LuckyLottery luckyLottery){
        return SqlHelper.retBool(luckyLotteryMapper.insert(luckyLottery));
    }

    /**
     * 根据期号与彩票类型查询最新一期的开奖信息，并补偿中间缺失的开奖信息
     * @param lotteryEnum 类型
     * @return
     */
    public LuckyLottery insertNextLotteryByType(LotteryEnum lotteryEnum){
        LuckyLottery latestLuckyLottery = searchLatestLotteryByType(lotteryEnum);
        if (latestLuckyLottery == null){
            log.info("暂无最近一期{}开奖信息，请初始化！", lotteryEnum.type);
            return null;
        }
        int round = 1;
        while (true){
            LuckyLottery nextLuckyLottery = FreeApiUtil.luckyLottery(latestLuckyLottery.getExpect() + round, lotteryEnum);
            if(nextLuckyLottery != null){
                log.info("开奖信息, 期号->{}, 类型->{}", nextLuckyLottery.getExpect(), lotteryEnum.type);
                insert(nextLuckyLottery);
                round++;
            }else {
                return nextLuckyLottery;
            }
        }
    }

    /**
     * 通知
     * @param luckyLottery
     * @return
     */
    public String describeForMarkdown(LuckyLottery luckyLottery){
        String result =
//                "> " + FreeApiUtil.dailyWord() + " \n" +
                        "## [幸运小助手]提醒您：" + luckyLottery.getExpect() + "-" + luckyLottery.getName() + "开奖了" + " \n" +
                        "开奖号码：" + luckyLottery.getOpenCode() + " \n" +
                        "![counting-money-in-the-dark](https://gitee.com/dearvainycos/my-pic/raw/master/img/counting-money-in-the-dark.jpg)";
        return result;
    }

}
